package com.yanqu.road.utils.serialize;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


public class StringToItemWeightList extends JsonDeserializer<List<List<ItemWeight>>> {
    @Override
    public List<List<ItemWeight>> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
        String text = jsonParser.getText();
        String[] split = text.split("\\|");
        List<List<ItemWeight>> list = new ArrayList<>();
        for (String s : split) {
            String[] split1 = s.split(";");
            List<ItemWeight> itemWeightList = new ArrayList<>();

            for (String s1 : split1) {
                String[] split2 = s1.split(",");
                if (split2.length != 2) {
                    continue;
                }
                ItemWeight itemWeight = new ItemWeight();
                Item item = Item.newInstance(split2[0]);
                itemWeight.setItem(item);
                itemWeight.setWeight(Integer.parseInt(split2[1]));
                itemWeightList.add(itemWeight);
            }

            list.add(itemWeightList);
        }
        return list;
    }
}
